package com.example.innovate.controller;

/*
    2021.9.13 update/
 */

import com.example.innovate.bean.Result;
import com.example.innovate.domain.AdminInfoDO;
import com.example.innovate.domain.StudentInfoDO;
import com.example.innovate.service.AdminService;
import com.example.innovate.service.NewFieldService;
import com.example.innovate.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student")
@Api(tags = "学生信息接口")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    AdminService adminService;
    @Autowired
    NewFieldService newFieldService;

    //@UserLoginToken
    @GetMapping("/findAllStudent")
       @ApiOperation("查找所有学生信息")
    public Result findAllStudent(@RequestParam("pageNumber") Integer pageNumber,
                                 @RequestParam("pageSize") Integer pageSize,
                                 @RequestParam("className") String className) throws Exception{
        if(className.equals("superAdmin")){
            List<StudentInfoDO> list = studentService.findAllStudent(pageNumber,pageSize);
            return new Result(200,"success","返回成功",list);
        }
        else{
            List<StudentInfoDO> list = studentService.findAllStudent(pageNumber,pageSize);
            return new Result(200,"success","返回成功",list);
        }

    }

    //@UserLoginToken
    @PostMapping("/addStudent")
    @ApiOperation("添加学生信息")
    public Result addStudent(@RequestBody String information,
                             @RequestParam("className") String className,
                             @RequestParam("userId") int userId) throws Exception{
        System.out.println(information);
        JSONObject jo = JSONObject.fromObject(information);
        System.out.println(jo);
        Object stuField = jo.get("stuField");
        String s = stuField.toString();
        jo.remove("stuField");
        StudentInfoDO studentInfoDO = (StudentInfoDO) JSONObject.toBean(jo,StudentInfoDO.class);
        studentInfoDO.setStuField(s);
        studentInfoDO.setStuUserId(userId);
        studentInfoDO.setStuClassName(className);
        studentService.addStudent(studentInfoDO);
        return new Result(200,"success","添加成功",studentInfoDO);
    }

    //@UserLoginToken
    @PutMapping("/updateStudent")
    @ApiOperation("修改学生信息")
    public Result updateStudent(@RequestBody String information) throws Exception{
        JSONObject jo = JSONObject.fromObject(information);
        Object stuField = jo.get("stuField");
        String s = stuField.toString();
        jo.remove("stuField");
        StudentInfoDO studentInfoDO = (StudentInfoDO) JSONObject.toBean(jo,StudentInfoDO.class);
        studentInfoDO.setStuField(s);
        StudentInfoDO studentInfoDO1 = studentService.findStudentById(studentInfoDO.getStuId());
        if(studentInfoDO1!=null){
            studentService.updateStudent(studentInfoDO);
            return new Result(200,"success","修改成功",studentInfoDO);
        }
        else{
            return new Result(400,"error","所更新学生信息不存在",null);
        }

    }

    //@UserLoginToken
    @DeleteMapping("/deleteStudent")
    @ApiOperation("删除学生信息")
    public Result deleteStudent(@RequestParam("id") int id) throws Exception{
        StudentInfoDO studentInfoDO = studentService.findStudentById(id);
        if(studentInfoDO!=null){
            studentService.deleteStudent(id);
            return new Result(200,"success","删除成功","id= "+id);
        }
        else{
            return new Result(400,"error","所删除学生信息不存在",null);
        }

    }

    //@UserLoginToken
//    @ApiOperation("学生信息Excel导入")
//    @PostMapping(value = "/import")
//    public Result importExcel(@RequestParam("file") MultipartFile file, @RequestParam("className") String className) throws IOException {
//        List<Student> list = ExcelUtils.importExcel(file,1,1,Student.class);
//        Student student = new Student();
//        if(className.equals("superAdmin")){
//            for(int i = 0 ; i < list.size();i++){
//                student = list.get(i);
//                student.setId(0);
//                studentService.addStudent(student);
//            }
//        }
//        else{
//            for(int i = 0 ; i < list.size();i++){
//                student = list.get(i);
//                student.setId(0);
//                student.setClassName(className);
//                studentService.addStudent(student);
//            }
//        }
//        return new Result(200,"success","导入成功",list);
//    }
//
//    @UserLoginToken
//    @ApiOperation("学生信息Excel导出")
//    @GetMapping("/export")
//    public Result export(HttpServletResponse response, @RequestParam("className") String className) throws IOException {
//        if(className.equals("superAdmin")){
//            List<Student> studentList = studentService.findAll();
//            for (int i = 0; i < studentList.size(); i ++){
//                studentList.get(i).setId(i+1);
//            }
//            ExcelUtils.exportExcel(studentList,"学生信息","sheet1",Student.class,"学生信息",response);
//            return new Result(200,"success","导出成功",null);
//        }
//        else{
//            List<Student> studentList = studentService.findAllByClass(className);
//            for (int i = 0; i < studentList.size(); i ++){
//                studentList.get(i).setId(i+1);
//            }
//            ExcelUtils.exportExcel(studentList,className+"学生信息","sheet1",Student.class,className+"学生信息",response);
//            return new Result(200,"success","导出成功",null);
//        }
//    }



    //@UserLoginToken
    @GetMapping("/findByName")
    @ApiOperation("根据姓名查找学生信息")
    public Result findByName(@RequestParam("name") String name) throws Exception {
        JSONArray array = studentService.valueList();
        JSONArray array1 = new JSONArray();
        for(int i=0; i<array.size(); i++){
            String s = (String) array.getJSONObject(i).get("name");
            if(s.equals(name)){
                array1.add(array.getJSONObject(i));
            }
        }
        if(array1!=null){
            return new Result(200,"success","查找完毕",array1);
        }
        else{
            return new Result(400,"error","搜索用户不存在","null");
        }

    }



}
